#include "app_ch_calib.h"
#include "app_settings.h"
#include "../hal/hal_link.h"
#include <stdio.h>

// 全局变量，存储 通道校准 界面对象
static lv_obj_t *ch_calib_screen;

// 当前界面索引
static int8_t current_page = 0;

// 界面容器
static lv_obj_t * page_containers[3];
// 屏幕宽度
static lv_coord_t screen_width;

static lv_obj_t *label_array_mid[4];
static lv_obj_t *label_array_min[4];
static lv_obj_t *label_array_max[4];

static void update_labels(void);
static void app_ch_calibjump_to_first_page(void);

/*----和硬件相关变量----*/
union 
{
    uint16_t buff[12];

    struct 
    {
        uint16_t adc_max[4];
        uint16_t adc_mid[4];
        uint16_t adc_min[4];
    };
} app_ch_calib;

void app_ch_calib_set_adc(uint16_t *buff)
{
    if (!is_screen_active(ch_calib_screen)) {
        return;
    }

    if (current_page == 0) {
        for (int i = 0; i < 4; i++) {
            app_ch_calib.adc_mid[i] = buff[i];
        }

        update_labels();
    }

    if (current_page == 1) {
        for (int i = 0; i < 4; i++) {

            if (buff[i] > app_ch_calib.adc_max[i]) {
                app_ch_calib.adc_max[i] = buff[i];
            }

            if (buff[i] < app_ch_calib.adc_min[i]) {
                app_ch_calib.adc_min[i] = buff[i];
            }
        }

        update_labels();
    }
}

// 更新标签内容的函数
static void update_labels(void)
{
    for (int i = 0; i < 4; i++) {
        char buf[10];
        snprintf(buf, sizeof(buf), "%u", app_ch_calib.adc_mid[i]);  
        lv_label_set_text(label_array_mid[i], buf);
    }

    for (int i = 0; i < 4; i++) {
        char buf[10];
        snprintf(buf, sizeof(buf), "%u", app_ch_calib.adc_min[i]);
        lv_label_set_text(label_array_min[i], buf);
    }

    for (int i = 0; i < 4; i++) {
        char buf[10];
        snprintf(buf, sizeof(buf), "%u", app_ch_calib.adc_max[i]);
        lv_label_set_text(label_array_max[i], buf);
    }
}

// 下一步按钮事件回调
static void next_btn_event_cb(lv_event_t * e) {
    lv_event_code_t code = lv_event_get_code(e);
    if (code == LV_EVENT_CLICKED) {
        if (current_page < 2) {
            // 创建滑动动画
            lv_anim_t a;
            lv_anim_init(&a);
            lv_anim_set_var(&a, page_containers[current_page]);
            lv_anim_set_values(&a, 0, -screen_width);
            lv_anim_set_time(&a, 300);
            lv_anim_set_path_cb(&a, lv_anim_path_linear);
            lv_anim_set_exec_cb(&a, (lv_anim_exec_xcb_t)lv_obj_set_x);
            lv_anim_start(&a);

            lv_obj_clear_flag(page_containers[current_page + 1], LV_OBJ_FLAG_HIDDEN);
            lv_obj_set_x(page_containers[current_page + 1], screen_width);
            lv_anim_t a_next;
            lv_anim_init(&a_next);
            lv_anim_set_var(&a_next, page_containers[current_page + 1]);
            lv_anim_set_values(&a_next, screen_width, 0);
            lv_anim_set_time(&a_next, 300);
            lv_anim_set_path_cb(&a_next, lv_anim_path_linear);
            lv_anim_set_exec_cb(&a_next, (lv_anim_exec_xcb_t)lv_obj_set_x);
            lv_anim_start(&a_next);

            current_page++;
        }
    }
}

// 上一步按钮事件回调
static void prev_btn_event_cb(lv_event_t * e) {
    lv_event_code_t code = lv_event_get_code(e);
    if (code == LV_EVENT_CLICKED) {
        if (current_page > 0) {
            // 创建滑动动画
            lv_anim_t a;
            lv_anim_init(&a);
            lv_anim_set_var(&a, page_containers[current_page]);
            lv_anim_set_values(&a, 0, screen_width);
            lv_anim_set_time(&a, 300);
            lv_anim_set_path_cb(&a, lv_anim_path_linear);
            lv_anim_set_exec_cb(&a, (lv_anim_exec_xcb_t)lv_obj_set_x);
            lv_anim_start(&a);

            lv_obj_clear_flag(page_containers[current_page - 1], LV_OBJ_FLAG_HIDDEN);
            lv_obj_set_x(page_containers[current_page - 1], -screen_width);
            lv_anim_t a_prev;
            lv_anim_init(&a_prev);
            lv_anim_set_var(&a_prev, page_containers[current_page - 1]);
            lv_anim_set_values(&a_prev, -screen_width, 0);
            lv_anim_set_time(&a_prev, 300);
            lv_anim_set_path_cb(&a_prev, lv_anim_path_linear);
            lv_anim_set_exec_cb(&a_prev, (lv_anim_exec_xcb_t)lv_obj_set_x);
            lv_anim_start(&a_prev);

            current_page--;
        }
    }
}

// 跳转到第一个界面的函数
static void app_ch_calibjump_to_first_page(void)
{
    if (current_page != 0) {
        // 隐藏当前界面
        lv_obj_add_flag(page_containers[current_page], LV_OBJ_FLAG_HIDDEN);
        // 显示第一个界面
        lv_obj_clear_flag(page_containers[0], LV_OBJ_FLAG_HIDDEN);
        // 设置第一个界面的位置
        lv_obj_set_x(page_containers[0], 0);
        // 更新当前界面索引
        current_page = 0;
    }
}

// 创建遥控器校准界面
static void show_remote_calibration_ui(void)
{
    // 初始化标签样式
    static lv_style_t style_label;
    lv_style_init(&style_label);

    // 设置标签文本颜色为白色
    lv_style_set_text_color(&style_label, lv_color_black());
    lv_style_set_text_font(&style_label, &font_20_alibaba);

    screen_width = lv_disp_get_hor_res(NULL);

    // 创建三个界面容器
    for (int i = 0; i < 3; i++) {
        page_containers[i] = lv_obj_create(ch_calib_screen);
        lv_obj_align(page_containers[i], LV_ALIGN_CENTER, 0, 30);

        lv_obj_set_size(page_containers[i], lv_pct(80), lv_pct(80));
        if (i != 0) {
            lv_obj_add_flag(page_containers[i], LV_OBJ_FLAG_HIDDEN);
        }
    }

    // 第一个界面
    // 创建提示标签
    lv_obj_t * label1 = lv_label_create(page_containers[0]);
    lv_label_set_text(label1, "请将摇杆移动到中点，并且保持不动点击下一步");
    lv_obj_set_height(label1, 100); // 固定宽度
    lv_obj_center(label1);
    lv_obj_add_style(label1, &style_label, 0);
    lv_obj_align(label1, LV_ALIGN_TOP_MID, 0, 0);

    // 创建下一步按钮
    lv_obj_t * next_btn1 = lv_btn_create(page_containers[0]);
    lv_obj_add_event_cb(next_btn1, next_btn_event_cb, LV_EVENT_ALL, NULL);
    lv_obj_align(next_btn1, LV_ALIGN_BOTTOM_RIGHT, 0, -20);
    lv_obj_t * next_label1 = lv_label_create(next_btn1);
    lv_label_set_text(next_label1, "下一步");

    // 第二个界面
    // 创建提示标签
    lv_obj_t * label2 = lv_label_create(page_containers[1]);
    lv_label_set_text(label2, "请滑动摇杆，让软件找出最大值和最小值后点击下一步");
    lv_obj_set_height(label2, 100);
    lv_obj_center(label2);
    lv_obj_add_style(label2, &style_label, 0);
    lv_obj_align(label2, LV_ALIGN_TOP_MID, 0, 0);

    // 创建上一步按钮
    lv_obj_t * prev_btn2 = lv_btn_create(page_containers[1]);
    lv_obj_add_event_cb(prev_btn2, prev_btn_event_cb, LV_EVENT_ALL, NULL);
    lv_obj_align(prev_btn2, LV_ALIGN_BOTTOM_LEFT, 20, -20);
    lv_obj_t * prev_label2 = lv_label_create(prev_btn2);
    lv_label_set_text(prev_label2, "上一步");

    // 创建下一步按钮
    lv_obj_t * next_btn2 = lv_btn_create(page_containers[1]);
    lv_obj_add_event_cb(next_btn2, next_btn_event_cb, LV_EVENT_ALL, NULL);
    lv_obj_align(next_btn2, LV_ALIGN_BOTTOM_RIGHT, -20, -20);
    lv_obj_t * next_label2 = lv_label_create(next_btn2);
    lv_label_set_text(next_label2, "下一步");

    // 第三个界面
    // 创建提示标签
    lv_obj_t * label3 = lv_label_create(page_containers[2]);
    lv_label_set_text(label3, "恭喜你校准成功!!!");
    lv_obj_set_height(label3, 100);
    lv_obj_center(label3);
    lv_obj_add_style(label3, &style_label, 0);
    // lv_obj_align(label3, LV_ALIGN_TOP_MID, 0, 0);
}

static void show_remote_adc(void)
{
    // 初始化标签样式
    static lv_style_t style_label;
    lv_style_init(&style_label);

    // 设置标签文本颜色为白色
    lv_style_set_text_color(&style_label, lv_color_black());
    lv_style_set_text_font(&style_label, &font_20_alibaba);

    /* 第一个界面 */
    const char *label_text[] = {
        "ch1:",
        "ch2:",
        "ch3:",
        "ch4:",
    };

    lv_obj_t * label_last;

    for (int i = 0; i < 4; i++) {
        lv_obj_t * label;
        if (i == 0) {
            label = lv_label_create(page_containers[0]);
            lv_label_set_text(label, label_text[i]);
            lv_obj_align(label, LV_ALIGN_LEFT_MID, 100, -80);
            lv_obj_add_style(label, &style_label, 0);
        } else {
            label = lv_label_create(page_containers[0]);
            lv_label_set_text(label, label_text[i]);
            lv_obj_align_to(label, label_last, LV_ALIGN_OUT_BOTTOM_LEFT, 0, 30);
            lv_obj_add_style(label, &style_label, 0);
        }

        label_last = label;
    }

    for (int i = 0; i < 4; i++) {
        label_array_mid[i] = lv_label_create(page_containers[0]);
        lv_obj_add_style(label_array_mid[i], &style_label, 0);
        if (i == 0) {
            lv_obj_align_to(label_array_mid[i], label_last, LV_ALIGN_RIGHT_MID, 120, -160);
        } else {
            lv_obj_align_to(label_array_mid[i], label_last, LV_ALIGN_OUT_BOTTOM_LEFT, 0, 30);
        }
        label_last = label_array_mid[i];
    }
    

    /* 第二个界面 */
    for (int i = 0; i < 4; i++) {
        lv_obj_t * label;
        if (i == 0) {
            label = lv_label_create(page_containers[1]);
            lv_label_set_text(label, label_text[i]);
            lv_obj_align(label, LV_ALIGN_LEFT_MID, 100, -80);
            lv_obj_add_style(label, &style_label, 0);
        } else {
            label = lv_label_create(page_containers[1]);
            lv_label_set_text(label, label_text[i]);
            lv_obj_align_to(label, label_last, LV_ALIGN_OUT_BOTTOM_LEFT, 0, 30);
            lv_obj_add_style(label, &style_label, 0);
        }
        label_last = label;
    }

    lv_obj_t *label_max = lv_label_create(page_containers[1]);
    lv_label_set_text(label_max, "max");
    lv_obj_align_to(label_max, label_last, LV_ALIGN_RIGHT_MID, 120, -215);
    lv_obj_add_style(label_max, &style_label, 0);

    lv_obj_t *label_min = lv_label_create(page_containers[1]);
    lv_label_set_text(label_min, "min");
    lv_obj_align_to(label_min, label_max, LV_ALIGN_RIGHT_MID, 220, 0);
    lv_obj_add_style(label_min, &style_label, 0);

    label_last = label_max;
    for (int i = 0; i < 4; i++) {
        label_array_max[i] = lv_label_create(page_containers[1]);
        lv_obj_add_style(label_array_max[i], &style_label, 0);
        lv_obj_align_to(label_array_max[i], label_last, LV_ALIGN_OUT_BOTTOM_LEFT, 0, 30);
        label_last = label_array_max[i];
    }

    label_last = label_min;
    for (int i = 0; i < 4; i++) {
        label_array_min[i] = lv_label_create(page_containers[1]);
        lv_obj_add_style(label_array_min[i], &style_label, 0);
        lv_obj_align_to(label_array_min[i], label_last, LV_ALIGN_OUT_BOTTOM_LEFT, 0, 30);
        label_last = label_array_min[i];
    }
}

static void ch_calib_init(void)
{
    for (int i = 0; i < 4; i++) {
        app_ch_calib.adc_max[i] = 0;
        app_ch_calib.adc_min[i] = 0xffff;
    }
    
    app_ch_calibjump_to_first_page();
}

static void ch_back_to_set_event_cb(lv_event_t *e)
{
    lv_obj_t *btn = lv_event_get_target(e);
    (void)btn;

    if (current_page == 2) {
        hal_link->tx_calib_buff(app_ch_calib.buff);
        //printf("sosn%d  %d  %d  %d\n", app_ch_calib.buff[0], app_ch_calib.buff[1], app_ch_calib.buff[2], app_ch_calib.buff[3]);
    }
    
    lv_scr_load_anim(settings_screen_get(), LV_SCR_LOAD_ANIM_FADE_ON, 200, 0, false);
}

void ch_calib_screen_init(void)
{
    ch_calib_screen = lv_obj_create(NULL);

    show_return_base("Channel Calibration", ch_calib_screen, ch_back_to_set_event_cb);

    show_remote_calibration_ui();

    show_remote_adc();
}

lv_obj_t *ch_calib_screen_get(void)
{
    return ch_calib_screen;
}

lv_obj_t *ch_calib_screen_get_have_init(void)
{
    ch_calib_init();

    return ch_calib_screen;
}